package com.sdry.web.controller.jyy.simple;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.jyy.Receive;
import com.sdry.model.jyy.ReceiveDetail;
import com.sdry.model.jyy.SimpleJyy;
import com.sdry.model.jyy.Workload;
import com.sdry.model.jyy.unpack.JyyUnpacking;
import com.sdry.model.jyy.unpack.UnpackDetal;
import com.sdry.model.llm.DevaningLog;
import com.sdry.model.llm.DevaningWork;
import com.sdry.model.lz.Customer;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.jyy.ReceiveDetailService;
import com.sdry.service.jyy.ReceiveService;
import com.sdry.service.jyy.SimpleServise;
import com.sdry.service.jyy.unpack.JyyUnpackingService;
import com.sdry.service.jyy.unpack.UnpackDetalService;
import com.sdry.service.llm.DevaningWorkService;
import com.sdry.service.lz.StockOutOrderService;
import com.sdry.service.zc.ZcInventoryManagementService;
import com.sdry.service.zc.ZcSimplificationInService;
import com.sdry.utils.DateJsonValueProcessor;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;

/**
 * 恒安精简库存操作（出入库、上下架）
 * @author jyy
 * @version 1.0
 */
@Controller
@RequestMapping("/simple")
public class SimpleConttroller {
	
	/**批量库存操作业务层*/
	@Resource
	private SimpleServise simpleServise;
	/**收货单业务层*/
	@Resource 
	private ReceiveService receiveService;
	@Resource 
	private ReceiveDetailService receiveDetailService;
	@Resource
	private ZcSimplificationInService zcSimplificationInService;
	@Resource
	private ZcInventoryManagementService zcInventoryManagementService;
	@Resource
	private StockOutOrderService stockOutOrderService;
	/**拆箱单据*/
	@Resource
	private JyyUnpackingService jyyUnpackingService;
	/**拆箱详情*/
	@Resource
	private UnpackDetalService unpackDetalService;
	/**拆箱工作量*/
	@Resource
	private DevaningWorkService devaningWorkService;
	/**拆箱业务层*/
	@Resource JyyUnpackingService unpackingService;
	
	
	//----------------------------------------------------------------入库---------------------------------------------------------------------
	/**
	 * 去批量入库页面
	 * @param response 响应
	 * @return 批量入库页面
	 */
	@RequestMapping("/toSimple")
	public String toSimple( Model model, HttpServletResponse response){
		//查询所有供应商
		List<Customer> allCustomerList = simpleServise.selectAllCustomerListByCodeType();
		model.addAttribute("allCustomerList",allCustomerList);
		return "jyy/simpleJyy";
	}
	
	/**
	 * 查询批量入库单
	 * @param param 入库（收货单）
	 * @param response 响应
	 */
	@RequestMapping("/selectReceiveList")
	public void selectReceiveList(Receive param, HttpServletResponse response) {
		//查询条件
		param.setIsSimple("2");
		//列表
		List<Receive> list = receiveService.findPageByMution(param);
		//总数
		Integer total = receiveService.selectCountByMution(param);
		
		JsonConfig jsonConfig = new JsonConfig();
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		try {
			ResponseUtil.write(response, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增入库单
	 * @param receive
	 * @param httpServletResponse
	 */
	@RequestMapping("insertSimplificationIn")
	public void insertSimplificationIn(Receive receive, HttpServletResponse response, HttpSession httpSession){
		ZcSysUserEntity su = (ZcSysUserEntity)httpSession.getAttribute("user");
		receive.setCreateName(su.getId()+"");
		
		receive.setSendDate(receive.getCreateDate());
		receive.setState(0);
		receive.setIsSimple("2");
		//int affact = zcSimplificationInService.insertSimplificationIn(receive);
		Long affact = receiveService.insert(receive);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 新增详情
	 * @param receiveDetail
	 * @param response
	 */
	@RequestMapping("insertDetails")
	public void insertDetails(ReceiveDetail receiveDetail, HttpServletResponse response){
		//获取当前日期为批次
		String pici = DateUtil.dateFormat();
		receiveDetail.setPici(pici);
		int affact = zcSimplificationInService.insertDetails(receiveDetail);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 收货提交
	 * @param receiveNumber 收货单号
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/submitReceiptWeb")
	public void submitReceiptWeb(String receiveNumber, HttpServletResponse response, HttpSession httpSession) throws Exception{
		//初始化影响行数
		int affact = 0;
		//获取session中的用户信息
		ZcSysUserEntity user = (ZcSysUserEntity)httpSession.getAttribute("user");
		
		//根据收货单号查询收货单明细
		ReceiveDetail param = new ReceiveDetail();
		param.setReceiveNumber(receiveNumber);
		List<ReceiveDetail> receiveDetailList = receiveDetailService.queryAllByMution(param);
		
		//遍历修改
		for (ReceiveDetail receiveDetail : receiveDetailList) {
			/*==================修改收货详细表信息==========*/
			receiveDetail.setCodeNum(receiveDetail.getTotalNum());
			affact = receiveDetailService.update(receiveDetail);
			/*====================添加库存=======================*/
			
			//查询数据库中是否存在同一物料，同一批次，同一库位的情况
			SimpleJyy simpleJyy = new SimpleJyy();
			simpleJyy.setMid(receiveDetail.getMid());
			simpleJyy.setmBatch(receiveDetail.getPici());
			simpleJyy.setLocation("HC");
			simpleJyy.setmNum(receiveDetail.getCodeNum());
			simpleJyy.setEnterPerson(user.getId());
			simpleJyy.setEnterDate(DateUtil.dateFormat1());
			
			simpleJyy.setIsUnpacking("0");
			
			affact = up(simpleJyy);
		}

		if(affact >0 ) {
			//更改一级状态  --为已收货
			Receive receive = new Receive();
			receive.setReceiveNumber(receiveNumber);
			receive.setIsSimple("2");
			List<Receive> receives = receiveService.allByMution(receive);
			
			if(receives.size()  > 0) {
				Receive receive2 = receives.get(0);
				receive2.setState(1);
				receive2.setReceiveDate(DateUtil.dateFormat1());
				receive2.setReceiveName(user.getId()+"");
				receive2.setSendCompany(null);
				affact = receiveService.update(receive2);
			}
		}
		
		ResponseUtil.write(response, affact);
	}
	
	//---------------------------------------------------------收货-app--------------------------------------------------------------
	
	/**
	 * 查询状态为0的计数入库单
	 * @param response
	 */
	@RequestMapping("selectAllReceipt")
	public void selectAllReceipt(HttpServletResponse response){
		Receive param = new Receive();
		param.setIsSimple("2");
		param.setState(0);
		//未收货
		List<Receive> list = receiveService.queryAllByMution(param );
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 收货提交(app)
	 * @param Details 收货详情集合
	 * @param person 收货人
	 * @param sure_name 回收容器数量
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/submitReceipt")
	public void submitReceipt(String Details, String person, String sure_name, HttpServletResponse response) throws Exception{
		//初始化影响行数
		int affact = 0;
		//json参数转化为实体集合
		Gson gson = new Gson();
		List<ReceiveDetail> receiveDetailList = gson.fromJson(Details, new TypeToken<List<ReceiveDetail>>() {}.getType());
		//遍历修改
		for (ReceiveDetail receiveDetail : receiveDetailList) {
			/*==================新表添加数据================*/
			//ReceiveDetailService.addReceiveDetailInstock(receiveDetail);
			/*==================修改收货详细表信息==========*/
			affact = receiveDetailService.update(receiveDetail);
			
			/*====================添加库存=======================*/
			//查询数据库中是否存在同一物料，同一批次，同一库位的情况
			SimpleJyy simpleJyy = new SimpleJyy();
			simpleJyy.setMid(receiveDetail.getMid());
			simpleJyy.setmBatch(receiveDetail.getPici());
			simpleJyy.setLocation("HC");
			simpleJyy.setmNum(receiveDetail.getCodeNum());
			simpleJyy.setEnterPerson(Long.valueOf(person));
			simpleJyy.setEnterDate(DateUtil.dateFormat1());
			simpleJyy.setIsUnpacking("0");
			affact = up(simpleJyy);
		}

		//获取收货单号
		ReceiveDetail receiveDetail = receiveDetailList.get(0);
		//更改一级状态  --为已收货
		Receive receive = new Receive();
		receive.setReceiveNumber(receiveDetail.getReceiveNumber());
		receive.setIsSimple("2");
		List<Receive> receives = receiveService.allByMution(receive);
		
		if(receives.size()  > 0) {
			Receive receive2 = receives.get(0);
			receive2.setState(1);
			receive2.setReceiveDate(DateUtil.dateFormat1());
			receive2.setReceiveName(person);
			receive2.setSendCompany(null);
			//回收容器数量
			receive2.setSureName(sure_name);
			receiveService.update(receive2);
		}
		JSONObject jsonObject = new JSONObject();
		if (affact > 0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "提交成功");
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "提交失败");
		    jsonObject.put("data", "0");
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	
	//----------------------------------------------------------------上架---------------------------------------------------------------------

	/**
	 * 根据产品码查询缓存区物料信息，为上架做准备
	 * @param sqd 产品码
	 * @param response 响应
	 */
	@RequestMapping("/selectMaFromHc")
	public void selectMaFromHc(String sqd, HttpServletResponse response){
		List<SimpleJyy> list = simpleServise.selectMaFromHc(sqd);
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 精简入库上架---提交
	 * @param onlyCount 精简仓库实体
	 */
	@RequestMapping("/simpleUp")
	public void simpleUp(String json, HttpServletResponse response, HttpSession httpSession) {
		
		//json转实体
		Gson gson = new Gson();
		SimpleJyy simpleJyy = gson.fromJson(json, SimpleJyy.class);
		simpleJyy.setEnterDate(DateUtil.dateFormat1());
		//初始化插入或修改后的影响行数
		int affact = up(simpleJyy);
		
		//减去缓存区中的数量
		if(affact > 0) {
			simpleJyy.setLocation("HC");
			affact = simpleServise.simpleDown(simpleJyy);
		}
		
		JSONObject jsonObject = new JSONObject();
		if(affact > 0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "上架成功");
		} else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "上架失败");
		    jsonObject.put("data", "0");
		}
		
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//----------------------------------------------------------------下架---------------------------------------------------------------------
	
	/**
	 * 为下架在库存中根据物料产品码和库位查询
	 * @param sqd 产品码
	 * @param location 库位（可为空）
	 */
	@RequestMapping("/selectMaFromDown")
	public void selectMaFromDown(String sqd, String location, HttpServletResponse response) {
		
		//新建实体存放多个查询条件
		SimpleJyy simpleJyy = new SimpleJyy();
		//借用此字段
		simpleJyy.setRemark(sqd);
		simpleJyy.setLocation(location);
		List<SimpleJyy> list = simpleServise.selectMaFromDown(simpleJyy);
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 精简入库下架---提交
	 * （可适应一个库位放多种物料的情况，可适应一个库位全部下架，或部分下架等多种情况）
	 * @param list 下架的所有信息
	 * @param response 响应
	 */
	@RequestMapping("/simpleDown")
	public void simpleDown(String jsonArray, HttpServletResponse response) {
		
		//1.将json转化成实体集合
		Gson gson = new Gson();
		List<SimpleJyy> list = gson.fromJson(jsonArray, new TypeToken<List<SimpleJyy>>() {}.getType());
		
		//2.下架
		int affact = simpleServise.allSimpleDown(list);
		
		//3.放到缓存区
		for (SimpleJyy simpleJyy : list) {
			simpleJyy.setLocation("HC");
			
			//调用上架公用方法
			affact = up(simpleJyy);
		}
		
		JSONObject jsonObject = new JSONObject();
		if(affact > 0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", "下架成功");
		} else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "下架失败");
		    jsonObject.put("data", "0");
		}
		
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//----------------------------------------------------------------拆箱------------------------------------------	
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/toSimpleUnpack")
	public String toSimpleUnpack (Model model) {
		//查询所有供应商
		List<Customer> allCustomerList = simpleServise.selectAllCustomerListByCodeType();
		model.addAttribute("customer_type0", allCustomerList);
		return "/jyy/simpleUnpack";
	}
	
	
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(JyyUnpacking param,HttpServletResponse response) throws Exception{
		
		param.setType("1");
		
		//条件分页查询内容
		List<JyyUnpacking> list = unpackingService.findPageByMution(param);
		//条件查询总条数
		Integer total = unpackingService.selectCountByMution(param);
		//转为json数据传至页面
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		ResponseUtil.write(response, result);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(JyyUnpacking param, HttpServletResponse response, HttpSession httpSession) throws Exception{
		
		//获取当前操作人
		ZcSysUserEntity su = (ZcSysUserEntity)httpSession.getAttribute("user");
		Long uid = su.getId();
		
		param.setCreateDate(DateUtil.dateFormat3());
		param.setCreateName(uid);
		param.setState(0);
		param.setType("1");
		Long id = unpackingService.insert(param);
		response.getWriter().print(id);
	}
	
	
	/**
	 * 根据供应商id在批量库存中查询未拆箱的物料及数量
	 * @param customerId 供应商id
	 */
	@RequestMapping("/selectPackingMa")
	public void selectPackingMa(Long customerId, HttpServletResponse response){
		List<SimpleJyy> list = simpleServise.selectPackingMa(customerId);
		
		JsonConfig jsonConfig = new JsonConfig();
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", list.size());
		try {
			ResponseUtil.write(response, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 给app查询所有未拆箱及未完成的拆箱单
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping("/selectAllUnpacking")
	public void selectAllReceive(HttpServletResponse response) throws Exception {
		UnpackDetal param = new UnpackDetal();
		param.setType("1");
		List<UnpackDetal> list = unpackDetalService.queryAllByMution(param );
		
		JSONObject jsonObject = new JSONObject();
		int count = list.size();
		if (count>0) {
			//传递数据到页面
		    jsonObject.put("status", "1");
		    jsonObject.put("message", "");
		    jsonObject.put("data", list);
		}else {
			//传递数据到页面
		    jsonObject.put("status", "0");
		    jsonObject.put("message", "查询数据为空");
		    jsonObject.put("data", list);
		}
		ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * 拆箱
	 * @param num 实际拆箱总数
	 * @param userId 拆箱工作量实体
	 * @param unpackDetail 拆箱详情
	 * @param response             
	 */
	@RequestMapping("/devanning")
	public void devanning(String num, String userId, String unpackDetail, HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		//影响行数
		Integer affct  = 0;
		//实际拆箱总数
		int snum = 0;
		//拆箱任务总数
		int znum  = 0;
		Gson gson = new Gson();
		
		//拆箱详情
		UnpackDetal unpackDetal2=  gson.fromJson(unpackDetail, UnpackDetal.class);
		Long did = unpackDetal2.getId();
		if(!"".equals(unpackDetal2.getZnum()) && null != unpackDetal2.getZnum()) {
			znum = unpackDetal2.getZnum();
		}
		
		snum = Integer.parseInt(num);
	
		//工作量
		List<Workload> workList = gson.fromJson(userId, new TypeToken<List<Workload>>() {}.getType());
		String workman = workList.get(0).getWorkman();
		//根据员工姓名查询员工id
		Long uid = unpackDetalService.selectUidByUname(workman);
		
		SimpleJyy simpleJyy = new SimpleJyy();
		simpleJyy.setLocation("HC");
		simpleJyy.setMid(unpackDetal2.getMid());
		simpleJyy.setmBatch(unpackDetal2.getPici());
		simpleJyy.setmNum(snum);
		simpleJyy.setIsUnpacking("0");
		simpleJyy.setEnterPerson(uid);
		simpleJyy.setEnterDate(DateUtil.dateFormat3());
		
		//下架前，先查询，缓存区可拆数量够不够，不够给出提示，下够以后再拆箱
		SimpleJyy selectByAll = simpleServise.selectByAll(simpleJyy);
		if(selectByAll != null) {
			if(selectByAll.getmNum() != null) {
				//库存缓存区可拆数量
				Integer getmNum = selectByAll.getmNum();
				if(getmNum > snum) {
					//下架（从缓存区将拆箱前数量减去）
					int affact = simpleServise.simpleDown2(simpleJyy);
					if(affact > 0){
						//上架（从缓存区将下架的物料上到缓存区，状态为：拆箱后）
						simpleJyy.setIsUnpacking("1");
						affct = up(simpleJyy);
						if (affct > 0) {
							//**********************************************工作量****************************************************************			
							Date date = new Date();
							DevaningLog dl = new DevaningLog();
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							String time = sdf.format(date);
							dl.setWork_time(time);
							SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
							String time2 = sdf2.format(date);
							dl.setNumber(time2);
							
							//保存工作量
							DevaningWork dw = new DevaningWork();
							dw.setUserName(workman);
							dw.setLogNumber(time2);
							for(Workload work : workList){
								dw.setBoxQuantity(work.getGood_num());
								dw.setPcsQuantity(work.getNgood_num());
								devaningWorkService.insert(dw);
							}
						//********************************************工作量****************************************************************	
						////////////////////////////////////////////////jyy-拆箱单据处理-start////////////////////////////////////////////////////////////////////		
							//更新拆箱详情中拆箱数量
							UnpackDetal unpackDetal = new UnpackDetal();
							//详情id
							unpackDetal.setId(Long.valueOf(did));
							
							snum = unpackDetal2.getUnum()+snum;
							//拆箱数量
							unpackDetal.setUnum(snum);
							
							if( znum > snum) {
								unpackDetal.setState(1);
							}else if(znum == snum){
								unpackDetal.setState(2);
							}
							
							unpackDetalService.update(unpackDetal);
							
							//根据单号查询拆箱计划单信息
							JyyUnpacking param = new JyyUnpacking();
							param.setOrdernum(unpackDetal2.getOrdernum());
							List<JyyUnpacking> JyyUnpackings = jyyUnpackingService.queryAllByMution(param );
							if(JyyUnpackings.size() > 0) {
								JyyUnpacking jyyUnpacking = JyyUnpackings.get(0);
								Long id = jyyUnpacking.getId();
								param.setId(id);
								
								//如果，单子中没有，则添加拆箱人，拆箱时间
								if("".equals(jyyUnpacking.getUnpackName()) || null == jyyUnpacking.getUnpackName() ) {
									
									param.setUnpackName(uid);
									param.setUnpackDate(DateUtil.dateFormat1());
								}
								//根据单号查询此单子的所有详情
								UnpackDetal unpackDetalQery = new UnpackDetal();
								unpackDetalQery.setOrdernum(jyyUnpacking.getOrdernum());
								//此查询结果为：所有状态不为2的详情集合
								List<UnpackDetal> queryAllByMutions = unpackDetalService.queryAllByMution(unpackDetalQery );
								//判断是否有不为2的详情
								if(queryAllByMutions.size() > 0) {
									//有：修改状态为未完成
									param.setState(1);
								}else{
									//没有，则修改单子的状态为2
									param.setState(2);
								}
								//修改单子的状态及拆箱信息
								affct = jyyUnpackingService.update(param);
								/////////////////////////////////////////////////jyy-拆箱单据处理-end////////////////////////////////////////////////////////////////////				
													
								if (affct > 0) {
								//传递数据到页面
								jsonObject.put("status", "1");
								jsonObject.put("message", "");
								jsonObject.put("data", "提交成功");
								}else {
								//传递数据到页面
								jsonObject.put("status", "0");
								jsonObject.put("message", "提交失败");
								jsonObject.put("data", "0");
								}
							}
						}
					}else {
						//传递数据到页面
						jsonObject.put("status", "0");
						jsonObject.put("message", "从缓存区减拆箱前数量错误");
						jsonObject.put("data", "0");
					}
				}else {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "缓存区可拆箱数量不够，请先下架");
					jsonObject.put("data", "0");
				}
			}else {
				//传递数据到页面
				jsonObject.put("status", "0");
				jsonObject.put("message", "缓存区可拆箱数量不够，请先下架");
				jsonObject.put("data", "0");
			}
		}else {
			//传递数据到页面
			jsonObject.put("status", "0");
			jsonObject.put("message", "缓存区可拆箱数量不够，请先下架");
			jsonObject.put("data", "0");
		}
		
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//----------------------------------------------------------------公用方法-------------------------------------------
	
	/**
	 * 上架，公用
	 * @param simpleJyy 批量库存实体
	 * @return 影响行数
	 */
	public int up(SimpleJyy simpleJyy) {
		int affact = 0;
		
		//查询数据库中是否存在同一物料，同一批次，同一库位,同一状态（拆箱前、后）的情况
		SimpleJyy onlyCountAlredy= simpleServise.selectByAll(simpleJyy );
		if(onlyCountAlredy != null) {
			//存在，则数量累加
			
			//原库位数量
			Integer getmNum = onlyCountAlredy.getmNum();
			//新上架库位数量
			Integer getmNumNew = simpleJyy.getmNum();
			onlyCountAlredy.setmNum(getmNum + getmNumNew);
			
			//修改库存数量
			affact = simpleServise.updateOnlyCount(onlyCountAlredy);
		}else {
			//不存在，添加一条记录
			affact = simpleServise.insertOnlyCount(simpleJyy);
		}
		
		return affact;
	}
	///////////////////////////////////////////批量库存列表展示////////////////////////////////////////////////////////
	@RequestMapping("/pageSimple")
	public String pageTotalStock(Model model,HttpSession session){
        //查询所有供应商
        List<Customer> customer_type0 = stockOutOrderService.queryAllCustomerByType();
        model.addAttribute("allCustomerList",customer_type0);
        ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
		 if(staf.getRoleId().equals("20010,")) {
			 Long customer_id = staf.getCustomer_id();
			 model.addAttribute("customer_id",customer_id);
		 }
        return "/jyy/simple";
    }
	/**
	 * 根据条件分页查询批量库存信息
	 * @param simpleJyy 条件
	 * @param response 良品库库存集合
	 */
	@RequestMapping("/selectSimpleJyyPage")
	public void selectAllGood(SimpleJyy simpleJyy, HttpServletResponse response) {
		//带条件的分页查询 良品库存
		List<SimpleJyy> list = simpleServise.findSimpleJyyPageByMution(simpleJyy);
		//统计 良品库库存 总记录数
		Integer total = simpleServise.selectSimpleJyyCountByMution(simpleJyy);
		
		JsonConfig jsonConfig = new JsonConfig();
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		try {
			ResponseUtil.write(response, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
